import pandas as pd
import json

# 读取CSV文件
file_path = '../PointDownLoad/A6顶板底钢筋网_相对坐标系.csv'
df = pd.read_csv(file_path)

# 创建一个新的DataFrame来保存横筋和纵筋的坐标
columns = ['meshNo', 'Size', 'angle01', 'length01', 'angle02', 'length02', 'Type', 'x_start', 'y_start', 'x_end',
           'y_end']
coordinates_df = pd.DataFrame(columns=columns)

# 提取横筋的坐标
horizontal_rebars = df[df['Type'] == 1]
horizontal_coords = horizontal_rebars[
    ['Mesh No', 'Size', 'Start Angle', 'Start Length', 'End Angle', 'End Length', 'Type', 'x1', 'y1', 'x2',
     'y2']].copy()
horizontal_coords.columns = ['meshNo', 'Size', 'angle01', 'length01', 'angle02', 'length02', 'Type', 'x_start',
                             'y_start', 'x_end', 'y_end']

# 提取纵筋的坐标
vertical_rebars = df[df['Type'] == 0]
vertical_coords = vertical_rebars[
    ['Mesh No', 'Size', 'Start Angle', 'Start Length', 'End Angle', 'End Length', 'Type', 'x2', 'y2', 'x5',
     'y5']].copy()
vertical_coords.columns = ['meshNo', 'Size', 'angle01', 'length01', 'angle02', 'length02', 'Type', 'x_start',
                           'y_start', 'x_end', 'y_end']

# 合并横筋和纵筋的坐标
coordinates_df = pd.concat([horizontal_coords, vertical_coords], ignore_index=True)

# 初始化JSON结构
json_output = {
    "meshNo": 1,
    "planNum": 1,
    "finishNum": 0,
    "zdiameter": None,
    "angle01": None,
    "lengh01": None,
    "angle02": None,
    "lengh02": None,
    "angle03": None,
    "lengh03": None,
    "angle04": None,
    "lengh04": None,
    "bendPosition01": None,
    "bendPosition02": None,
    "originPoint": [0, 0],
    "hSubs": [],
    "zSubs": []
}

# 填充hSubs和zSubs字段
h_index = 1
z_index = 1
for _, row in coordinates_df.iterrows():
    if row["Type"] == 0:  # 横筋
        json_output["hSubs"].append({
            "index": h_index,
            "start": [int(row["x_start"]), int(row["y_start"])],
            "end": [int(row["x_end"]), int(row["y_end"])],
            "hDiameter ": row["Size"]
        })
        h_index += 1
    elif row["Type"] == 1:  # 纵筋
        json_output["zSubs"].append({
            "index": z_index,
            "start": [int(row["x_end"]), int(row["y_end"])],
            "end": [int(row["x_start"]), int(row["y_start"])]
        })
        z_index += 1

# 将结果保存为JSON文件
output_file_path = 'A6顶板底钢筋网.json'
with open(output_file_path, 'w') as json_file:
    json.dump(json_output, json_file, indent=4)
